package MydbQA::Command::Base;

use strict;
use warnings;
use Carp;
use HTML::Template;

use MySQL::Query;
use MySQL::Explain;
use MySQL::Query::Hints;
use MySQL::Indexes;
use MySQL::Query::Tables;
use MySQL::Query::OrderOfExecution;
use MySQL::Query::Extract;

use base qw(Class::Accessor::Fast);
__PACKAGE__->mk_accessors(qw( dbh query analyze_commands tree explain hints extract indexes tables ooe sql_builder ));

sub new {
      my ($proto, %args) = @_;
      my $class = ref($proto) || $proto;    
      my $self = $class->SUPER::new( \%args );
      $self->init;
      return $self;
}

sub init {
      my $self = shift;
      croak "No query defined\n" unless $self->query;
      croak "Database connection reqired for that command\n" unless ref $self->dbh;
      
      my $tree = MySQL::Query->new( sql => $self->query );
      $self->tree($tree);
      
      my $sql_builder = $tree->as_builder;
      $self->sql_builder($sql_builder);
      
      my $explain = MySQL::Explain->new(dbh => $self->dbh);
      $self->explain($explain);
      
      my $hints = MySQL::Query::Hints->new();
      $self->hints($hints);
      
      my $indexes = MySQL::Indexes->new(dbh => $self->dbh);
      $self->indexes($indexes);
      
      my $tables = MySQL::Query::Tables->new(tree => $tree);
      $self->tables($tables);
      
      my $ooe = MySQL::Query::OrderOfExecution->new();
      $self->ooe($ooe);
      
      my $extract = MySQL::Query::Extract->new(tree => $tree , tables => $tables); 
      $self->extract($extract);
      
      $self->analyze_commands(qw(Ooe Tables Indexes Columns));
}



1;

